package com.hundun.vision.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.binance.connector.futures.client.impl.UMFuturesClientImpl;
import com.hundun.vision.biz.dto.KlineBarDTO;
import com.hundun.vision.biz.dto.SymbolDTO;
import com.hundun.vision.biz.request.KlineRequest;
import com.hundun.vision.biz.service.MarketService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author ：RuoChen
 * @date ：16:10 2024/11/28
 * @description：市场服务实现类
 */
@Service
@Slf4j
public class MarketServiceImpl implements MarketService {
    @Value("${binance.service_url}")
    private String serviceUrl;

    @Override
    public SymbolDTO getSymbol(String symbol) {
        SymbolDTO pair;
        List<SymbolDTO> symbols;
        UMFuturesClientImpl client = new UMFuturesClientImpl(serviceUrl);
        String result = client.market().exchangeInfo();
        JSONObject data = JSONObject.parseObject(result);
        symbols = JSON.parseArray(data.getString("symbols"), SymbolDTO.class);
        pair = symbols.stream().filter(item -> item.getSymbol().equals(symbol.toUpperCase())).findFirst().orElse(null);
        return pair;
    }

    @Override
    public List<SymbolDTO> getSymbols() {
        List<SymbolDTO> symbols;
        UMFuturesClientImpl client = new UMFuturesClientImpl(serviceUrl);
        String result = client.market().exchangeInfo();
        JSONObject data = JSONObject.parseObject(result);
        symbols = JSON.parseArray(data.getString("symbols"), SymbolDTO.class);
        return symbols;
    }

    @Override
    public List<KlineBarDTO> getKlineBar(KlineRequest request) {
        List<KlineBarDTO> klineBars = new ArrayList<>();
        UMFuturesClientImpl client = new UMFuturesClientImpl(serviceUrl);
        LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
        parameters.put("symbol", request.getSymbol());
        parameters.put("interval", request.getInterval());
        parameters.put("startTime", request.getStartTime());
        parameters.put("endTime", request.getEndTime());
        parameters.put("timeZone", request.getTimeZone());
        parameters.put("limit", request.getLimit());
        String result = client.market().klines(parameters);
        if (StringUtils.isNotEmpty(result)) {
            JSONArray jsonArray = JSON.parseArray(result);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONArray innerArray = jsonArray.getJSONArray(i);
                KlineBarDTO klineBar = new KlineBarDTO();
                klineBar.setStartTimestamp(innerArray.getLong(0));
                klineBar.setOpenPrice(innerArray.getBigDecimal(1));
                klineBar.setHighPrice(innerArray.getBigDecimal(2));
                klineBar.setLowPrice(innerArray.getBigDecimal(3));
                klineBar.setClosePrice(innerArray.getBigDecimal(4));
                klineBar.setVolume(innerArray.getBigDecimal(5));
                klineBar.setEndTimestamp(innerArray.getLong(6));
                klineBar.setAmount(innerArray.getBigDecimal(7));
                klineBars.add(klineBar);
            }
        }
        return klineBars;
    }

    @Override
    public BigDecimal getMarketPrice(String symbol) {
        UMFuturesClientImpl client = new UMFuturesClientImpl(serviceUrl);
        LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
        parameters.put("symbol", symbol);
        String result = client.market().markPrice(parameters);
        JSONObject data = JSON.parseObject(result);
        return data.getBigDecimal("markPrice");
    }
}
